﻿using Engine;
using Game;
using Mekiasm.ParticleSystem;

namespace Mekiasm
{
    public class Flamethrower : ItemFlat
    {
        public int eachUse = 0;
        public Common.SoundItem SoundItem = new Common.SoundItem("flamethroweractive");
        public Flamethrower() : base("火焰喷射器", MekiasmInit.GetFlatTexture(MekiasmInit.R2, "火焰喷射器"))
        {

        }

        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            return false;
        }

        public override bool OnAim(Ray3 aim, ComponentMiner componentMiner, AimState state)
        {
            if (SaveItemSystem.GetBlockData(componentMiner.ActiveBlockValue, out Common.SaveItem toolSaveItem) && toolSaveItem.fuelAmount > 0)
            {
                SaveItemSystem.ChangePower(componentMiner.Inventory, componentMiner.Inventory.ActiveSlotIndex, -1, toolSaveItem.fuelValue);
                object obj = componentMiner.Raycast(aim, RaycastMode.Interaction, false, true, false);
                SoundItem.Play();
                SubsystemMekBlockBehavior.CacheTasks.Add(() =>
                {
                    if (ILibrary.SubsystemTime.PeriodicGameTimeEvent(1.0, 0.0))
                    {
                        SoundItem.Pause(); return true;
                    }
                    else return false;
                });
                ILibrary.SubsystemParticles.AddParticleSystem(new FlameThrown(ILibrary.SubsystemTerrain, componentMiner.ComponentPlayer.ComponentBody.Position + new Vector3(aim.Direction.X, 1f, aim.Direction.Z), aim.Direction));
                if (obj is TerrainRaycastResult)
                {
                    CellFace cellFace = ((TerrainRaycastResult)obj).CellFace;
                    float dis = Vector3.Distance(componentMiner.ComponentPlayer.ComponentBody.Position, new Vector3(cellFace.Point));
                    if (dis > 7f && !ILibrary.SubsystemTime.PeriodicGameTimeEvent(0.4f, 0.0f)) return true;
                    ILibrary.Project.FindSubsystem<SubsystemExplosivesBlockBehavior>().IgniteFuse(cellFace.X, cellFace.Y, cellFace.Z);
                    ILibrary.Project.FindSubsystem<SubsystemFireBlockBehavior>().SetCellOnFire(cellFace.X, cellFace.Y, cellFace.Z, 1f);
                }
                else if (obj is BodyRaycastResult)
                {
                    BodyRaycastResult? bodyRaycastResult = (BodyRaycastResult)obj;
                    float dis = Vector3.Distance(componentMiner.ComponentPlayer.ComponentBody.Position, bodyRaycastResult.Value.ComponentBody.Position);
                    if (dis > 7f && !ILibrary.SubsystemTime.PeriodicGameTimeEvent(0.4f, 0.0f)) return true;
                    ComponentHealth componentHealth = bodyRaycastResult.Value.ComponentBody.Entity.FindComponent<ComponentHealth>();
                    if (componentHealth != null) componentHealth.Injure(0.05f,componentMiner.ComponentCreature, false, "被火焰喷射器烧死");
                    ComponentOnFire componentOnFire = bodyRaycastResult.Value.ComponentBody.Entity.FindComponent<ComponentOnFire>();
                    if (componentOnFire != null)
                    {
                        componentOnFire.SetOnFire(componentMiner.ComponentCreature, ILibrary.random.Float(6f, 8f));
                    }
                }
            }
            return true;
        }


        public override int GetMaxPower()
        {
            return 10240;
        }

        public override bool IsCrushWhenNoPower()
        {
            return false;
        }
        public override int GetBasePower()
        {
            return 0;
        }

    }
}
